function validateForm(form, callback) {
	var $form = $(form);
	if (!$form.valid()) {
	    return false;
	}    
 }
 function validateCallback(form, callback) {
     /*
        dwz:xheditor+validate 二次提交解决办法
     */
     var $p = $(document);
     if ($.fn.xheditor) {
         $('textarea.editor', $p).each(function () {
             var $this = $(this);
             $this.val($this.val());
         })
     }
     //End
	var $form = $(form);
	if (!$form.valid()) {
		return false;
}
	$.ajax({
		type: form.method || 'POST',
		url: $form.attr("action"),
		contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		data: $form.serializeArray(),
		dataType:"json",
		cache: false,
		success: callback || DWZ.ajaxDone,
		error: DWZ.ajaxError
	});
	return false;
}
function iframeCallback(form, callback){
	var $form = $(form), $iframe = $("#callbackframe");
	if(!$form.valid()) {return false;}

	if ($iframe.size() == 0) {
		$iframe = $("<iframe id='callbackframe' name='callbackframe' src='about:blank' style='display:none'></iframe>").appendTo("body");
	}
	if(!form.ajax) {
		$form.append('<input type="hidden" name="ajax" value="1" />');
	}
	form.target = "callbackframe";
	
	_iframeResponse($iframe[0], callback || DWZ.ajaxDone);
}
function _iframeResponse(iframe, callback){
	var $iframe = $(iframe), $document = $(document);
	
	$document.trigger("ajaxStart");
	
	$iframe.bind("load", function(event){
		$iframe.unbind("load");
		$document.trigger("ajaxStop");
		
		if (iframe.src == "javascript:'%3Chtml%3E%3C/html%3E';" || // For Safari
			iframe.src == "javascript:'<html></html>';") { // For FF, IE
			return;
		}

		var doc = iframe.contentDocument || iframe.document;

		// fixing Opera 9.26,10.00
		if (doc.readyState && doc.readyState != 'complete') return; 
		// fixing Opera 9.64
		if (doc.body && doc.body.innerHTML == "false") return;
	   
		var response;
		
		if (doc.XMLDocument) {
			// response is a xml document Internet Explorer property
			response = doc.XMLDocument;
		} else if (doc.body){
			try{
				response = $iframe.contents().find("body").html();
				response = jQuery.parseJSON(response);
			} catch (e){ // response is html document or plain text
				response = doc.body.innerHTML;
			}
		} else {
			// response is a xml document
			response = doc;
		}
		
		callback(response);
	});
}

function navTabAjaxDone(json) {
    //alert("navTabAjaxDone in...");
    DWZ.ajaxDone(json);
    if (json.statusCode == DWZ.statusCode.ok) {
        if (json.navTabId) {
            navTab.reloadFlag(json.navTabId);
        } else {
            navTabPageBreak({}, json.rel);
        }
        if ("closeCurrent" == json.callbackType) {
            setTimeout(function () { navTab.closeCurrentTab(); }, 100);
            loadList();
        } else if ("forward" == json.callbackType) {
            if (json.forwardConfirm != undefined) {
                var str = json.forwardConfirm == '' || 'undefined' ? '操作成功！' : json.forwardConfirm;
                alertMsg.confirm(str, {
                    okCall: function () {
                        if ($('#navTab ul.navTab-tab').length > 0) {
                            navTab.reload(json.forwardUrl);
                        } else {
                            location.href = json.forwardUrl;
                        }
                    },
                    okName: '跳转到列表页',
                    cancelName: '返回当前页',
                    cancelCall: function () {
                        location.reload();
                    }
                });
            } else {
                if (json.message != '') {
                    alert(json.message)
                }
                window.location.href = json.forwardUrl;
            }

        } else if ('reset' == json.callbackType) {
            $('form').each(function () {
                this.reset();
            })
        } else {
            if ($('#navTab ul.navTab-tab').length > 0) {
                navTab.reload(json.forwardUrl);
            } else {
                window.location.href = json.forwardUrl;
            }
        }
    }
}

function dialogAjaxDone(json){
	DWZ.ajaxDone(json);
	if (json.statusCode == DWZ.statusCode.ok){
		if (json.navTabId){
			navTab.reload(json.forwardUrl, {navTabId: json.navTabId});
		} else if (json.rel) {
			navTabPageBreak({}, json.rel);
		}
		$.pdialog.closeCurrent();
	}
}

function navTabSearch(form, navTabId){
	var $form = $(form);
	if (form[DWZ.pageInfo.pageNum]) form[DWZ.pageInfo.pageNum].value = 1;
	navTab.reload($form.attr('action'), {data: $form.serializeArray(), navTabId:navTabId});
	return false;
}
function dialogSearch(form){
	var $form = $(form);
	if (form[DWZ.pageInfo.pageNum]) form[DWZ.pageInfo.pageNum].value = 1;
	$.pdialog.reload($form.attr('action'), {data: $form.serializeArray()});
	return false;
}
function dwzSearch(form, targetType){
	if (targetType == "dialog") dialogSearch(form);
	else navTabSearch(form);
	return false;
}
function divSearch(form, rel){
	var $form = $(form);
	if (form[DWZ.pageInfo.pageNum]) form[DWZ.pageInfo.pageNum].value = 1;
	if (rel) {
		var $box = $("#" + rel);
		$box.ajaxUrl({
			type:"POST", url:$form.attr("action"), data: $form.serializeArray(), callback:function(){
				$box.find("[layoutH]").layoutH();
			}
		});
	}
	return false;
}

function _getPagerForm($parent, args) {
	var form = $("#pagerForm", $parent).get(0);

	if (form) {
		if (args["pageNum"]) form[DWZ.pageInfo.pageNum].value = args["pageNum"];
		if (args["numPerPage"]) form[DWZ.pageInfo.numPerPage].value = args["numPerPage"];
		if (args["orderField"]) form[DWZ.pageInfo.orderField].value = args["orderField"];
		if (args["orderDirection"] && form[DWZ.pageInfo.orderDirection]) form[DWZ.pageInfo.orderDirection].value = args["orderDirection"];
	}
	
	return form;
}

function dwzPageBreak(options){
	var op = $.extend({ targetType:"navTab", rel:"", data:{pageNum:"", numPerPage:"", orderField:"", orderDirection:""}, callback:null}, options);
	var $parent = op.targetType == "dialog" ? $.pdialog.getCurrent() : navTab.getCurrentPanel();

	if (op.rel) {
		var $box = $parent.find("#" + op.rel);
		var form = _getPagerForm($box, op.data);
		if (form) {
			$box.ajaxUrl({
				type:"POST", url:$(form).attr("action"), data: $(form).serializeArray(), callback:function(){
					$box.find("[layoutH]").layoutH();
				}
			});
		}
	} else {
		var form = _getPagerForm($parent, op.data);
		var params = $(form).serializeArray();
		
		if (op.targetType == "dialog") {
			if (form) $.pdialog.reload($(form).attr("action"), {data: params, callback: op.callback});
		} else {
			if (form) navTab.reload($(form).attr("action"), {data: params, callback: op.callback});
		}
	}
}

function navTabPageBreak(args, rel){
	dwzPageBreak({targetType:"navTab", rel:rel, data:args});
}

function dialogPageBreak(args, rel){
	dwzPageBreak({targetType:"dialog", rel:rel, data:args});
}


function ajaxTodo(url, callback){
	var $callback = callback || navTabAjaxDone;
	if (! $.isFunction($callback)) $callback = eval('(' + callback + ')');
	$.ajax({
		type:'POST',
		url:url,
		dataType:"json",
		cache: false,
		success: $callback,
		error: DWZ.ajaxError
	});
}


function uploadifyAllComplete(event, data){
	if (data.errors) {
		var msg = "The total number of files uploaded: "+data.filesUploaded+"\n"
			+ "The total number of errors while uploading: "+data.errors+"\n"
			+ "The total number of bytes uploaded: "+data.allBytesLoaded+"\n"
			+ "The average speed of all uploaded files: "+data.speed;
		alert("event:" + event + "\n" + msg);
	}
}

function uploadifyComplete(event, queueId, fileObj, response, data){
	DWZ.ajaxDone(DWZ.jsonEval(response));
}

function uploadifyError(event, queueId, fileObj, errorObj){
	alert("event:" + event + "\nqueueId:" + queueId + "\nfileObj.name:" 
		+ fileObj.name + "\nerrorObj.type:" + errorObj.type + "\nerrorObj.info:" + errorObj.info);
}


$.fn.extend({
    ajaxTodo: function () {
        return this.each(function () {
            var $this = $(this);
            $this.unbind('click',fnAjaxTodo).click(fnAjaxTodo);
            function fnAjaxTodo(event) {
                //var url = unescape($this.attr("href")).replaceTmById($(event.target).parents(".unitBox:first"));
                var url = $this.attr("href").replaceTmById($(event.target).parents("#navTab"));
                DWZ.debug(url); 
                if (!url.isFinishedTm()) {
                    alertMsg.error($this.attr("warn") || DWZ.msg("alertSelectMsg"));
                    return false;
                }
                var title = $this.attr("title");
                if (title) {
                    alertMsg.confirm(title, {
                        okCall: function () {
                            ajaxTodo(url, $this.attr("callback"));
                        }
                    });
                } else {
                    ajaxTodo(url, $this.attr("callback"));
                }
                event.preventDefault();
            }
        });
    },
    dwzExport: function () {
        function _doExport($this) {
            var $p = $this.attr("targetType") == "dialog" ? $.pdialog.getCurrent() : navTab.getCurrentPanel();
            var $form = $("#pagerForm", $p);
            var url = $this.attr("href");
            window.location = url + (url.indexOf('?') == -1 ? "?" : "&") + $form.serialize();
        }

        return this.each(function () {
            var $this = $(this);
            $this.click(function (event) {
                var title = $this.attr("title");
                if (title) {
                    alertMsg.confirm(title, {
                        okCall: function () { _doExport($this); }
                    });
                } else { _doExport($this); }

                event.preventDefault();
            });
        });
    }
});


//boss start
function deleteDone(json) {
    DWZ.ajaxDone(json);
    if (json.statusCode == DWZ.statusCode.ok) {
        loadList(0);
    }
}